package org.sl.flvplayer.elements {
	import org.sl.flvplayer.data.Config;
	import org.sl.flvplayer.elements.IControllerElement;

	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.geom.ColorTransform;

	/**
	 * Provides a hilightcolor effect for all button elements on mouseOver and mouseOut.
	 * 
	 * @package org.sl.flvplayer.elements
	 * @author Dan
	 * @version
	 * @see org.sl.flvplayer.controls.IControllerElement
	 * @see org.sl.flvplayer.controls.Controller
	 */ 
	public class ControllerElement extends MovieClip implements IControllerElement {
		
		/**
		 * command which is bound to the element
		 */
		public var command : String;
		
		/**
		 * indicates whether the element is scaleable or not
		 */
		public var allowScale : Boolean;
		
		/**
		 * displayobject as a base to attach and show the different skins
		 */
		public var aktSkin : DisplayObject;
		
		/**
		 * contains the skins
		 */
		public var skinList : Array = new Array();

		private var isOver : Boolean = false;

		/**
		 * ControllerElement constructor adds the necessary listeners and ensures that only the button element is mouse sensitive.
		 * 	
		 * @param p_allowScale indicates whether the element is scaleable
		 */
		public function ControllerElement(p_allowScale : Boolean = false) {
			super();
			allowScale = p_allowScale;
			mouseChildren = false;			
			addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
		}

		/**
		 */
		public function addSkin(p_skin : MovieClip, p_type : String) : void {
			var obj : Object = {skin:p_skin, type:p_type};
			skinList.push(obj);
		}

		/**
		 */
		public function showSkin(p_type : String) : void {
			if (aktSkin) removeChild(aktSkin);
			for (var i:String in skinList) {
				if (skinList[i].type == p_type) {
					aktSkin = addChild(skinList[i].skin);
				}
			}
			// when the buttonskin changes and the mouse is still over the button this prevents the reset of the higlightcolor 
			if(isOver) {
				changeColor(Config.getInstance().highlightcolor);
			}
		}	

		
		/**
		 * Initializes the highlightcolor element and changes its color on mouseOver.
		 * 
		 * @param p_color colorcode as <code>Number</code>
		 */
		public function changeColor(p_color : Number) : void {
			var color_mc : MovieClip = MovieClip(aktSkin).getChildByName("color") as MovieClip;
			if (color_mc) {
				var	colorTransform : ColorTransform = color_mc.transform.colorTransform;		
				colorTransform.color = p_color;
				color_mc.transform.colorTransform = colorTransform;
			}
		}

		/**
		 * Handles the mouseOver event.
		 * 
		 * @param p_event
		 */
		protected function onMouseOver(p_event : MouseEvent) : void {
			isOver = true;
			changeColor(Config.getInstance().highlightcolor);
		}

		
		/**
		 * Handles the mouseOut event. The default colorcode for the highlightcolor elements is 0x333333.
		 * 
		 * @param p_event
		 */
		protected function onMouseOut(p_event : MouseEvent) : void {
			isOver = false;
			//default color 0x333333
			changeColor(0x333333);
		}	
	}
}
